<?php

namespace app\admin\controller\tools;

use app\admin\controller\Controllers;
use app\admin\service\tools\Account as AccountService;
use app\admin\service\tools\AccountCategory;
use app\admin\model\tools\Account as AccountModels;

class Account extends Controllers
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(AccountService $service, AccountCategory $accountCategory)
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\tools\Account.index');
        $list = $service->indexPaginate($get);
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total(),
            'category' => $accountCategory->lists(),
            'account' => $service->account($get)
        ]);

        return $this->fetch('tools/account/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create(AccountCategory $accountCategory)
    {
        $this->assign([
            'category' => $accountCategory->lists()
        ]);
        return $this->fetch('tools/account/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(AccountService $service)
    {
        $post = $this->request->only(['cid','price','datetime','desc'], 'post');
        $this->validate($post, 'app\admin\validate\tools\Account.create');
        return $service->create($post)?
            success():
            failed('新增失败');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(AccountModels $model, AccountCategory $accountCategory)
    {
        $this->assign([
            'data' => $model,
            'category' => $accountCategory->lists()
        ]);
        return $this->fetch('tools/account/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(AccountModels $model, AccountService $service)
    {
        $put = $this->request->only(['cid','price','datetime','desc'], 'put');
        $this->validate($put, 'app\admin\validate\tools\Account.update');
        return $service->save($model, $put)?
            success():
            failed('编辑失败');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(int $id, AccountService $service)
    {
        $this->validate(['id'=>$id], 'app\admin\validate\tools\Account.delete');
        return $service->destory($id)?
            success():
            failed('删除失败');
    }

    /**
     * 批量删除
     * @author zx
     * @date 2019-12-09
     * @return \think\Response
     */
    public function destory(AccountService $service)
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\tools\Account.destory');
        return $service->destory($delete['ids'])?
            success():
            failed('删除失败');
    }

}
